package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Schedule;
import com.easy.bean.TrainRoute;
import com.easy.mapper.ScheduleMapper;
import com.easy.service.ScheduleService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    ScheduleMapper scheduleMapper;


    @Override
    @LogMethod(serviceName = "添加班次", recordParam = true, recordReturn = true)
    public Schedule add(Schedule schedule) {
        scheduleMapper.add(schedule);
        Schedule result = scheduleMapper.one(schedule.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "删除班次", recordParam = true, recordReturn = true)
    @CacheEvict(value = "schedule", key = "#p0" ,allEntries = true)//清空value制定的所有实例
    public int del(Integer id) {
        return scheduleMapper.del(id);
    }

    @Override
    @LogMethod(serviceName = "修改班次", recordParam = true, recordReturn = true)
    @CachePut(value = "schedule", key = "#p0.id")
    public Schedule edit(Schedule schedule) {
        scheduleMapper.edit(schedule);
        Schedule result = scheduleMapper.one(schedule.getId());
        return result;
    }

    @Override
    @Cacheable(value = "schedule", key = "#p0")//也可以#p0表示第一个参数
    public Schedule one(Integer id) {
        return scheduleMapper.one(id);
    }


    @Override
    public void page(Page page, Schedule schedule) {
        int count = scheduleMapper.pageCount(schedule);
        Object data = scheduleMapper.page(page,schedule);
        page.setCount(count);
        page.setData(data);
    }








}
